import { alertModal } from '@/components/Modal'
import { UserInfoProps } from '@/utils/interface'
import { checkEmail, checkFax, checkIsUnsignedInteger, checkMobile, checkName } from '@/utils/rules'
import { useContext, useEffect, useImperativeHandle, useState } from 'react'
import { ProfileViewProps } from '../interface'
import { querySaveProfile } from '@/api/setApi'
import { formatDateZero, formatTDate } from '@/utils/common'
import { AppContext } from '@/pages/App/context'
import { Message } from '@kedacom/react-next'
const useProfileHook = (props: ProfileViewProps, ref: any) => {
	const INPUT_WIDTH = 300
	const EXT_NUM_WIDTD = 95
	const AppData = useContext(AppContext)
	const [oldData, setOldData] = useState<UserInfoProps>({
		moid: '',
		account: '', //账号
		name: '', //姓名
		email: '', //电子邮箱
		mobile: '', //电话
		extNum: '', //分机
		sex: '', //性别1 0-女；1-男；
		dateOfBirth: null, //生日
		fax: '', //传真
		seat: '', //办公位置
		deptPositions: '', //职位
	})
	const [formData, setFormData] = useState<UserInfoProps>({
		moid: '',
		account: '', //账号
		name: '', //姓名
		email: '', //电子邮箱
		mobile: '', //电话
		extNum1: '',
		extNum2: '',
		extNum3: '',
		extNum: '', //分机
		sex: '', //性别1 0-女；1-男；
		dateOfBirth: null, //生日
		fax: '', //传真
		seat: '', //办公位置
		deptPositions: '', //职位
	})
	const sexData = [
		{ text: '男', value: '1', width: 100 },
		{ text: '女', value: '0' },
	]
	useEffect(() => {
		if (props.profileInfo) {
			backInfo()
		}
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [props.profileInfo])
	useEffect(() => {
		let extNum = (formData.extNum1 || '') + '-' + (formData.extNum2 || '') + '-' + (formData.extNum3 || '')
		setFormData((v) => ({
			...v,
			extNum: extNum,
		}))
	}, [formData.extNum1, formData.extNum2, formData.extNum3])
	// 暴露组件的方法
	useImperativeHandle(ref, () => ({
		/**
		 * 表单的保存
		 */
		saveData: () => {
			toSave()
		},
	}))
	/**
	 * 回填信息
	 */
	const backInfo = () => {
		let _data = props.profileInfo
		let extNum = _data.extNum || '--'
		let obj = {
			moid: _data.moid || '',
			account: _data.account || '', //账号
			name: _data.name || '', //姓名
			email: _data.email || '', //电子邮箱
			mobile: _data.mobile || '', //电话
			extNum: extNum || '', //分机
			sex: _data.sex || '', //性别1 0-女；1-男；
			dateOfBirth: _data.dateOfBirth ? _data.dateOfBirth : null, //生日
			fax: _data.fax || '', //传真
			seat: _data.seat || '', //办公位置
			deptPositions: _data.deptPositions || '', //职位
		}
		setFormData({
			...obj,
			extNum1: extNum ? extNum.split('-')[0] : '',
			extNum2: extNum ? extNum.split('-')[1] : '',
			extNum3: extNum ? extNum.split('-')[2] : '',
		})
		setOldData({
			...obj,
		})
	}
	/**
	 * 校验个人信息
	 * @returns true/false
	 */
	const checkProfile = () => {
		if (!formData.account) {
			alertModal({
				msg: '请输入账号',
			})
			return false
		}
		if (formData.name === '') {
			alertModal({
				msg: '请输入姓名',
			})
			return false
		} else {
			if (!checkName(formData.name || '')) {
				return false
			}
		}
		if (formData.email && !checkEmail(formData.email)) {
			return false
		}
		if (formData.mobile && !checkMobile(formData.mobile)) {
			return false
		}
		if (formData.extNum1 && !checkIsUnsignedInteger(formData.extNum1)) {
			alertModal({
				msg: '区号-电话-分机只能为数字',
			})
			return false
		}
		if (formData.extNum2 && !checkIsUnsignedInteger(formData.extNum2)) {
			alertModal({
				msg: '区号-电话-分机只能为数字',
			})
			return false
		}
		if (formData.extNum3 && !checkIsUnsignedInteger(formData.extNum3)) {
			alertModal({
				msg: '区号-电话-分机只能为数字',
			})
			return false
		}
		if (formData.fax && !checkFax(formData.fax)) {
			return false
		}
		return true
	}
	const toSave = () => {
		let flag = checkProfile()
		if (!flag) {
			return false
		}
		let m = Message.loading('loading')
		let bir = formatDateZero(formData.dateOfBirth || '')
		let data = {
			date_of_birth: formData.dateOfBirth ? formatTDate(bir) : formData.dateOfBirth,
			moid: formData.moid,
			account: formData.account, //账号
			name: formData.name, //姓名
			email: formData.email, //电子邮箱
			mobile: formData.mobile, //电话
			ext_num: formData.extNum, //分机
			extNum1: formData.extNum ? formData.extNum.split('-')[0] : '',
			extNum2: formData.extNum ? formData.extNum.split('-')[1] : '',
			extNum3: formData.extNum ? formData.extNum.split('-')[2] : '',
			sex: formData.sex, //性别1 0-女；1-男；
			fax: formData.fax, //传真
			seat: formData.seat, //办公位置
			dept_positions: formData.deptPositions, //职位
		}
		querySaveProfile(data)
			.then(() => {
				m.hide()
				alertModal({
					msg: '修改成功',
				})
				/**修改了个人信息，尤其是姓名后要更新系统头部的信息，头部的姓名要同步修改更新 */
				AppData.reLoadUser()
				props.reloadProfileData({ isShowLoading: false })
			})
			.catch(() => {
				m.hide()
			})
	}
	const onInputChange = (v: string, name: string) => {
		setFormData({
			...formData,
			[name]: v,
		})
	}
	const onBirthChange = (newValue: any, formatString?: string) => {
		setFormData({
			...formData,
			dateOfBirth: formatString || null,
		})
	}
	return {
		oldData,
		INPUT_WIDTH,
		formData,
		onInputChange,
		sexData,
		onBirthChange,
		EXT_NUM_WIDTD,
		toSave,
	}
}
export default useProfileHook
